package cn.com.qtgl.userfilters.comp.service;

import cn.com.qtgl.userfilters.model.entity.DbPositionItem;
import cn.com.qtgl.userfilters.model.entity.DbUserFilter;
import cn.com.qtgl.userfilters.model.entity.DbUserItem;
import cn.com.qtgl.userfilters.model.pojo.*;
import cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService;
import cn.melonlib.cores.commons.comp.utils.FilterTools;
import cn.melonlib.cores.identification.comp.messenger.MessengerUtils;
import cn.melonlib.cores.jpa.model.bean.impl.DefaultQuery;
import cn.melonlib.objects.maps.JsonMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

@Service
public class DbUserFiltersService extends WorkerBasicService<DbUserFilter> {

    @Resource
    private FilterTools filterTools;

    @Resource
    private MessengerUtils messengerUtils;

    public List<DbUserFilter> loadUserFilters(String userFilters){
        List<String> userFilterList=Arrays.stream(userFilters.split(",")).filter(UserFilters::validUserFilters).map(String::trim).distinct().toList();

        JsonMap<JsonMap<Object>> params=JsonMap.createMap(filterTools.createFilters().addIn("id", userFilterList).getConditionsMap());
        List<DbUserFilter> dbUserFilters=dao.<List<DbUserFilter>,DefaultQuery>search(new DefaultQuery(DbUserFilter.class, params))
                .getResult();
        List<String> dbUserFilterIds=dbUserFilters.stream().map(DbUserFilter::getId).toList();
        return Stream.concat(dbUserFilters.stream(),userFilterList.stream().filter(item->!dbUserFilterIds.contains(item)).flatMap(item->createStoreUserFilter(item).stream())).toList();
    }


    public List<DbUserFilter> createStoreUserFilter(String userFilter){
        List<UserFilter<? extends Triple>> userFilters=UserFilters.parseUserFilters(userFilter).getUserFilters();
        return userFilters.stream().map(uf -> switch (uf.getUserFilterType()){
            case POSITION -> {
                DbUserFilter dbUserFilter=new DbUserFilter();
                dbUserFilter.setUserFilter(uf.getBody());
                dbUserFilter.setUserFilterType(uf.getUserFilterType());
                try {
                    messengerUtils.setMessengers(dbUserFilter);
                    DbUserFilter saveFilter=dao.save(dbUserFilter).getResult();
                    if( uf.getTriple() instanceof PositionItem positionItem){
                        DbPositionItem dbPi=new DbPositionItem();
                        dbPi.setDbUserFilter(saveFilter);
                        dbPi.setPost(positionItem.getPost());
                        dbPi.setPostAdapter(positionItem.getPostAdapter());
                        dbPi.setDepartment(positionItem.getDepartment());
                        dbPi.setDepartmentAdapter(positionItem.getDepartmentAdapter());
                        messengerUtils.transMessengers(saveFilter,dbPi);
                        dao.save(dbPi);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                yield dbUserFilter;
            }
            default -> {
                DbUserFilter dbUserFilter=new DbUserFilter();
                dbUserFilter.setUserFilter(uf.getBody());
                try {
                    messengerUtils.setMessengers(dbUserFilter);
                    DbUserFilter saveFilter = dao.save(dbUserFilter).getResult();
                    dbUserFilter.setUserFilterType(uf.getUserFilterType());
                    if (uf.getTriple() instanceof UserItem userItem) {
                        DbUserItem dbUserItem = new DbUserItem();
                        dbUserItem.setDbUserFilter(saveFilter);
                        dbUserItem.setCode(userItem.getCode());
                        messengerUtils.transMessengers(saveFilter, dbUserItem);
                        dao.save(dbUserItem);
                    }
                }catch (Exception e){
                    throw new RuntimeException(e);
                }
                yield dbUserFilter;
            }
        }).toList();
    }
}
